use diesel::prelude::*;

use crate::{database::schema, route};

#[derive(Queryable, Selectable)]
#[diesel(table_name = schema::tag)]
#[diesel(check_for_backend(diesel::mysql::Mysql))]
pub struct Tag {
    pub id: i32,
    pub name: String,
    pub desc: String,
    pub avatar: String,
    pub slug: String,
    pub cid: i32,
}

#[derive(Insertable, AsChangeset)]
#[diesel(table_name = schema::tag)]
pub struct NewTag<'a> {
    pub name: &'a str,
    pub desc: &'a str,
    pub avatar: &'a str,
    pub slug: &'a str,
    pub cid: i32,
}

impl<'a> From<&'a route::form::tag::NewTagForm> for NewTag<'a> {
    fn from(tag: &'a route::form::tag::NewTagForm) -> Self {
        NewTag {
            name: &tag.name,
            desc: &tag.desc,
            avatar: &tag.avatar,
            slug: &tag.slug,
            cid: tag.cid,
        }
    }
}

impl<'a> From<&'a Tag> for NewTag<'a> {
    fn from(tag: &'a Tag) -> Self {
        NewTag {
            name: &tag.name,
            desc: &tag.desc,
            avatar: &tag.avatar,
            slug: &tag.slug,
            cid: tag.cid,
        }
    }
}
